/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * under the License.
 */
package shopping.management;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.commons.BaseAction;
import shopping.model.Catalog;
import shopping.model.Product;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright 2010 mazhao.
 * <p/>
 * User: mazhao
 * Date: 2010/11/27
 * Time: 20:07:38
 */
public class GroupAction extends BaseAction {
    // =================== for catalog list ===================
    private int currentCatalogId = 0;
    private List<Catalog> catalogPath = new ArrayList<Catalog>();
    private List<Catalog> subCatalog = new ArrayList<Catalog>();
    private List<Product> productList = new ArrayList<Product>();

    public List<Product> getProductList() {
        return productList;
    }

    @Action(value = "/management/grouplist",
        results={
            @Result(name="success", location="/WEB-INF/content/management/group.jsp")
        }
    )
    public String list() {
        // get current catalog id
        if(currentCatalogId == 0) {
            log.debug("set catalog id to default");
            currentCatalogId = ManagementConstants.CATALOG_ROOT_ID;
        }

        // get catalog path
        Catalog curCatalog = new Catalog();
        curCatalog.setId(currentCatalogId);

        // select path to current catalog
        catalogPath = curCatalog.pathToMe( );
        if(log.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder("  ");
            for(Catalog c : catalogPath) {
                log.debug("catalog path:" + sb.toString() + c);
                sb.append("  ");
            }
        }

        // select sub catalogs of current catalog
        subCatalog = curCatalog.subCatalogs();
        if(log.isDebugEnabled()) {
            for(Catalog c : subCatalog) {
                log.debug("sub catalog:" + c);
            }
        }
        productList = Product.getAllProductsOfCatalog(currentCatalogId);

        // get group list for current catalog id

        return SUCCESS;
    }

    public List<Catalog> getCatalogPath() {
        return catalogPath;
    }

    public List<Catalog> getSubCatalog() {
        return subCatalog;
    }



    public int getCurrentCatalogId() {
        return currentCatalogId;
    }

    public void setCurrentCatalogId(int currentCatalogId) {
        this.currentCatalogId = currentCatalogId;
    }
}
